﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.loto.Common;
using com.huigo.Framework.ServiceUtils;
using com.huigo.Framework.Passport;

namespace com.huigo.B2B.Web
{
    public partial class Register : System.Web.UI.Page
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string custName
        {
            get
            {
                return RequestString("userid");
            }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public string custPwd
        {
            get
            {
                return RequestString("userpwd");
            }
        }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string email
        {
            get
            {
                return RequestString("email");
            }
        }
        /// <summary>
        /// 联系人
        /// </summary>
        public string userName
        {
            get
            {
                return RequestString("name");
            }
        }
        /// <summary>
        /// 联系电话
        /// </summary>
        public string tel
        {
            get
            {
                return RequestString("tel");
            }
        }
        /// <summary>
        /// 了解途径
        /// </summary>
        public string from
        {
            get
            {
                return RequestString("way");
            }
        }
        /// <summary>
        /// 验证码
        /// </summary>
        public string code
        {
            get
            {
                return RequestString("txtIdentityCode");
            }
        }
        /// <summary>
        /// 其他联系方式
        /// </summary>
        public string other
        {
            get
            {
                return RequestString("other");
            }
        }
        /// <summary>
        /// 公司名称
        /// </summary>
        public string company
        {
            get
            {
                return RequestString("company");
            }
        }
        /// <summary>
        /// 公司地址
        /// </summary>
        public string address
        {
            get
            {
                return RequestString("address");
            }
        }
        /// <summary>
        /// 公司邮编
        /// </summary>
        public string mailcode
        {
            get
            {
                return RequestString("mailcode");
            }
        }
        /// <summary>
        /// 公司电话
        /// </summary>
        public string companyTel
        {
            get
            {
                return RequestString("companyTel");
            }
        }
        /// <summary>
        /// 公司网址
        /// </summary>
        public string companyUrl
        {
            get
            {
                return RequestString("companyUrl");
            }
        }
        /// <summary>
        /// 公司注册号
        /// </summary>
        public string compaynNum
        {
            get
            {
                return RequestString("compaynNum");
            }
        }
        /// <summary>
        /// 负责人姓名
        /// </summary>
        public string manger
        {
            get
            {
                return RequestString("manger");
            }
        }
        /// <summary>
        /// Request一个布尔值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool RequestBool(string name)
        {
            string v = Request[name];
            if (v == null)
            {
                return false;
            }
            else
            {
                try
                {
                    return bool.Parse(v);
                }
                catch
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 请求一个字符串，返回null时返回空字符串
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string RequestString(string name)
        {
            string v = Request[name];
            if (v == null)
            {
                v = string.Empty;
            }
            v = v.Replace("<", "&lt;");
            v = v.Replace(">", "&gt;");
            return v.Trim();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CustPassport.ChkCookieLogin())
            {
                Response.Redirect("/center/order/OrderList.aspx");
            }
            else
            {
                if (this.RequestBool("isPost"))
                {
                    HttpCookie cookie = Request.Cookies["vvvvalidccccode"];
                    if (string.IsNullOrEmpty(custName))
                    {
                        MessageBox.Show(this.Page, "请输入用户名！");
                        return;
                    }
                    else if (string.IsNullOrEmpty(custPwd))
                    {
                        MessageBox.Show(this.Page, "请输入密码！");
                        return;
                    }
                    else if (string.IsNullOrEmpty(email))
                    {
                        MessageBox.Show(this.Page, "请输入电子邮箱！");
                        return;
                    }
                    else if (string.IsNullOrEmpty(userName))
                    {
                        MessageBox.Show(this.Page, "请填写联系人姓名！");
                        return;
                    }
                    else if (string.IsNullOrEmpty(tel))
                    {
                        MessageBox.Show(this.Page, "请填写联系人电话！");
                        return;
                    }
                    else if (string.IsNullOrEmpty(from))
                    {
                        MessageBox.Show(this.Page, "请选择了解途径！");
                        return;
                    }
                    else if (cookie.Value != code)
                    {
                        MessageBox.Show(this.Page, "验证码输入错误！");
                        return;
                    }
                    else
                    {
                        string otherContacts = string.Empty;
                        if (other == "如:QQ 12345678")
                        {
                            otherContacts = string.Empty;
                        }
                        else
                        {
                            otherContacts = other;
                        }
                        string fromName = string.Empty;
                        if (from == "1")
                        {
                            fromName = "搜索引擎";
                        }
                        else if (from == "2")
                        {
                            fromName = "朋友介绍";
                        }
                        else if (from == "3")
                        {
                            fromName = "业务交流";
                        }
                        else if (from == "4")
                        {
                            fromName = "媒体";
                        }
                        else if (from == "5")
                        {
                            fromName = "宣传资料";
                        }
                        else
                        {
                            fromName = "其它";
                        }
                        List<RequestFuncParams> list = new List<RequestFuncParams>();
                        list.Add(new RequestFuncParams("custName", custName));
                        list.Add(new RequestFuncParams("pwd", custPwd));
                        list.Add(new RequestFuncParams("email", email));
                        list.Add(new RequestFuncParams("userName", userName));
                        list.Add(new RequestFuncParams("tel", tel));
                        list.Add(new RequestFuncParams("from", fromName));
                        list.Add(new RequestFuncParams("otherContacts", otherContacts));
                        list.Add(new RequestFuncParams("companyName", company));
                        list.Add(new RequestFuncParams("companyAddress", address));
                        list.Add(new RequestFuncParams("companyZip", mailcode));
                        list.Add(new RequestFuncParams("companyTel", companyTel));
                        list.Add(new RequestFuncParams("companyWebsit", companyUrl));
                        list.Add(new RequestFuncParams("companyReg", compaynNum));
                        list.Add(new RequestFuncParams("chargeName", manger));
                        BaseService service = new BaseService();
                        ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.INSERT_CUST_INFO, list);
                        if (parser != null && parser.RowsCount > 0)
                        {
                            CustPassport.Login(custName, custPwd, 14);

                            Response.Redirect("/center/order/OrderList.aspx");
                        }
                    }
                }
            }
        }
    }
}
